package com.yili.oa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.entity.TableBase;
import com.yili.oa.models.Notice;
import com.yili.oa.service.NoticeService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.request.NoticeReq;
import com.yili.oa.vo.response.NoticeRes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/22 0022
 * @email 200890949@qq.com
 */
@Controller
@RequestMapping("notice")
public class NoticeApi {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/getList")
    @ResponseBody
    public TableBase getList(SearchBase search){
        PageHelper.startPage(search.getPage(),search.getLimit());
        List<Notice> list = new ArrayList<>();
        list = noticeService.getList(search);
        PageInfo pageInfo = new PageInfo<>(list,search.getLimit());
        List<NoticeRes> result = new ArrayList<>();
        for (Notice notice:
             list) {
            NoticeRes noticeRes = new NoticeRes();
            BeanUtils.copyProperties(notice,noticeRes);
            if (notice.getNoticeType().equals(0)){
                noticeRes.setNoticeType("浮动公告");
            }else if(notice.getNoticeType().equals(1)){
                noticeRes.setNoticeType("日常消息");
            }else {
                noticeRes.setNoticeType("游戏帮助");
            }
            result.add(noticeRes);
        }
        TableBase tableBase = new TableBase();
        tableBase.setData(result);
        tableBase.setCode(0);
        tableBase.setMsg("");
        tableBase.setCount(pageInfo.getTotal());
        return tableBase;
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody NoticeReq noticeReq){
        if (noticeReq.getNoticeType()==null){
            throw new AjaxException("请选择消息类型");
        }
        if (StringUtils.isBlank(noticeReq.getNoticeContent())){
            throw new AjaxException("请输入消息内容");
        }
        if(noticeReq.getNoticeContent().length()>500){
            throw new AjaxException("超过字数");
        }
        if (noticeReq.getId()==null) {
            noticeService.add(noticeReq);
        }else {
            noticeService.update(noticeReq);
        }
        return "success";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/editButton/{noticeId}")
    public String addButton(@PathVariable("noticeId")String noticeid, ModelMap modelAndView){
        SearchBase searchBase = new SearchBase();
        searchBase.setId(noticeid);
        List<Notice> list =noticeService.getList(searchBase);
        Notice notice = new Notice();
        if (ListUtils.isNotNullOrEmpty(list)){
            notice=list.get(0);
        }
        modelAndView.addAttribute("info",notice);
        return "game/addNotice";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> deleteSignIn(@RequestBody List<Long> list){
        Map<String, Object> map = new HashMap<>();
//        signInService.delSignIn(list);
        noticeService.delNotice(list);
        map.put("result", "success");
        return map;
    }
}
